<?php
include("../conn.php");

// 需传参数
$userid = $_REQUEST[userid];
$type = $_REQUEST[type];
$longitude=$_REQUEST[longitude];
$latitude=$_REQUEST[latitude];
// 需传参数 end
// 月初到月末
$t = getdate(time());
$sdate = $t[year].'-'.$t[mon].'-'.'01 00:00:00';
$edate = $t[year].'-'.$t[mon].'-'.'31 23:59:59';
// 月初到月末 end
if ($userid != '' && $longitude != '' && $latitude != '' && $type != '') {
	if ($type == 0) {
		$query = mysql_query("select * from my_collect where userid=".$userid." and goodsid!=0");
		while ($row = mysql_fetch_assoc($query)) {
			$sql = mysql_query("select * from goods where goodsid=".$row[goodsid]);
			$goods = mysql_fetch_assoc($sql);
			$row[goodsid] = $goods[goodsid];
			$row[name] = $goods[title];
			$row[images] = $goods[images];
			$row[price] = $goods[price];
			$row[intro] = $goods[summary];
			$row[number] = $goods[xiaoliang];

			$my_collect[] = $row;
		}
	}elseif ($type == 1) {
		$query = mysql_query("select * from my_collect where userid=".$userid." and shopid!=0");
		while ($row = mysql_fetch_assoc($query)) {
			$sql = mysql_query("select *,(SQRT(POWER(MOD(ABS(longitude - {$longitude}),360),2) + POWER(ABS(latitude - {$latitude}),2))*80*1000) AS distance from shop where shopid=".$row[shopid]);
			$shop = mysql_fetch_assoc($sql);

			// 查询月销售量
			$sql= mysql_query("select count(*) as num from goodsorders where addtime>='$sdate' and addtime<='$edate' and shopid=".$row[shopid]." and state<>0 and state<>4");
			$num = mysql_fetch_assoc($sql);
			$number = $num[num];
			// 查询月销售量 end
			
			$row[name] = $shop[name];
			$row[images] = $shop[images];
			$row[pingfen] = $shop[pingfen];
			$row[number] = $number;
			$row[distance] = $shop[distance];
			$row[start_price] = $shop[start_price];
			$row[distribution_fee] = $shop[distribution_fee];
			$row[shop_intro] = $shop[huodong_miaoshu];
			
			$my_collect[] = $row;
		}
	}
	$my_collect = empty($my_collect) ? [] : $my_collect;
	$arr = array('code' => 100, 'tip' => '查询成功', 'my_collect' => $my_collect);
}else{
	$arr = array('code' => 200, 'tip' => '参数传入有误,需传参数:用户id=>userid,经度=>longitude,纬度=>latitude,收藏类型:type(0为商品收藏,1为商家收藏)');
}
echo json_encode($arr);